//
//  UIColor+Exs.swift
//  ttjx
//
//  Created by 文 on 2019/8/28.
//  Copyright © 2019 ttjx. All rights reserved.
//

import Foundation
import UIKit

extension UIColor {
    
    public static let THEME                  = WHColor_16(colorStr: "F10E0E")
    
    public static let COLOR_TEXT_BLACK2      = WHColor_16(colorStr: "222222")  // 文字
    public static let COLOR_TEXT_GREY        = WHColor_16(colorStr: "8B8B8B")
    public static let COLOR_TEXT_BLACK333    = WHColor_16(colorStr: "333333")
    public static let COLOR_TEXT_BLACK666    = WHColor_16(colorStr: "666666")
    public static let COLOR_TEXT_BLACK999    = WHColor_16(colorStr: "999999")
    
    public static let COLOR_LIGHT_GREY       = WHColor_16(colorStr: "f5f5f5")
    public static let COLOR_LINE_GREY        = WHColor_16(colorStr: "dadada")
    
    
    public static var COLOR_RANDOM           = WHColor_ARC()// WHColor_RGB(r: (CGFloat(arc4random() % 256)), g: (CGFloat(arc4random() % 256)), b: (CGFloat(arc4random() % 256)))

    
    class var randomColor:UIColor{
        get{
            let red  = CGFloat(arc4random()%256)/255.0
            let green = CGFloat(arc4random()%256)/255.0
            let blue  = CGFloat(arc4random()%256)/255.0
            
            return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
        }
    }

}

//随机色
public func WHColor_ARC() -> UIColor {
    let r = (CGFloat(arc4random() % 256))
    let g = (CGFloat(arc4random() % 256))
    let b = (CGFloat(arc4random() % 256))
    if #available(iOS 10.0, *) {
        return UIColor(displayP3Red: r/255.0, green: g/255.0, blue: b/255.0, alpha: 1)
    } else {
        // Fallback on earlier versions
        return UIColor.init(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: 1)
    }
}

public func WHColorWithAlpha(colorStr:String , alpha:CGFloat)->UIColor{
    var color = UIColor.red
    var cStr : String = colorStr.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()
    
    if cStr.hasPrefix("#") {
        let index = cStr.index(after: cStr.startIndex)
        cStr = String(cStr.prefix(upTo: index))
    }
    
    if cStr.count != 6 {
        return UIColor.black
    }
    //两种不同截取字符串的方法
    let rIndex = cStr.index(cStr.startIndex, offsetBy: 2)
    let rStr = String(cStr.prefix(upTo: rIndex))
    
    let gRange = cStr.index(cStr.startIndex, offsetBy: 2) ..< cStr.index(cStr.startIndex, offsetBy: 4)
    let gStr = cStr[gRange]
    
    let bIndex = cStr.index(cStr.endIndex, offsetBy: -2)
    let bStr = cStr.suffix(from: bIndex)
    
    color = UIColor.init(red: CGFloat(Float(changeToInt(numStr: rStr)) / 255), green: CGFloat(Float(changeToInt(numStr: String(gStr))) / 255), blue: CGFloat(Float(changeToInt(numStr: String(bStr))) / 255), alpha: alpha)
    
    return color
}

public func WHColor_TextBlack()->UIColor{
    return WHColor_16(colorStr: "#333333")
}

public func WHColor_LineGray()->UIColor{
    return WHColor_RGB(r: 202, g: 202, b: 202)
}

//MARK: -RGB颜色
public func WHColor_RGB(r: CGFloat,g: CGFloat,b: CGFloat) -> UIColor {
    if #available(iOS 10.0, *) {
        return UIColor(displayP3Red: r/255.0, green: g/255.0, blue: b/255.0, alpha: 1)
    } else {
        // Fallback on earlier versions
        return UIColor.init(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: 1)
    }
}
//MARK: -16进制颜色
public func WHColor_16(colorStr:String) -> UIColor {
    
    var color = UIColor.red
    var cStr : String = colorStr.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()
    
    if cStr.hasPrefix("#") {
        let index = cStr.index(after: cStr.startIndex)
        cStr = String(cStr.prefix(upTo: index))
    }
    
    if cStr.count != 6 {
        return UIColor.black
    }
    //两种不同截取字符串的方法
    let rIndex = cStr.index(cStr.startIndex, offsetBy: 2)
    let rStr = String(cStr.prefix(upTo: rIndex))
    
    let gRange = cStr.index(cStr.startIndex, offsetBy: 2) ..< cStr.index(cStr.startIndex, offsetBy: 4)
    let gStr = cStr[gRange]
    
    let bIndex = cStr.index(cStr.endIndex, offsetBy: -2)
    let bStr = cStr.suffix(from: bIndex)
    
    color = UIColor.init(red: CGFloat(Float(changeToInt(numStr: rStr)) / 255), green: CGFloat(Float(changeToInt(numStr: String(gStr))) / 255), blue: CGFloat(Float(changeToInt(numStr: String(bStr))) / 255), alpha: 1)
    
    return color
}

private func changeToInt(numStr:String) -> Int {
    
    let str = numStr.uppercased()
    var sum = 0
    for i in str.utf8 {
        //0-9 从48开始
        sum = sum * 16 + Int(i) - 48
        if i >= 65 {
            //A~Z 从65开始，但初始值为10
            sum -= 7
        }
    }
    return sum
}
